//
//  UIBarButtonItem+Extension.swift
//  HSLMicroBlog
//
//  Created by mac on 15/12/8.
//  Copyright © 2015年 HSL. All rights reserved.
//

import UIKit

let titleSize: CGFloat = 15.0
let titleColor: UIColor = UIColor(white: 80.0 / 255.0, alpha: 1.0)

extension UIBarButtonItem {
    //便利构造函数
    convenience init(imgName: String? = nil, title: String? = nil, target: AnyObject?, action: Selector) {
        self.init()
        
        let button = UIButton()
        button.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        if let img = imgName {
            button.setImage(UIImage(named: img), forState: .Normal)
            button.setImage(UIImage(named: "\(img)_highlighted"), forState: .Highlighted)
        }
        
        if let t = title {
            button.setTitle(t, forState: .Normal)
            button.setTitleColor(titleColor, forState: .Normal)
            button.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
            button.titleLabel?.font = UIFont.systemFontOfSize(titleSize)
        }
        
        button.sizeToFit()
        customView = button
        
    }
}